/**
 * input下拉框类
 */
;(function () {
	/**
	 * input
	 * content
	 * readonly
	 */
	function InputContents(options) {
		var icthis = this;
		//将选项合并到InputContents对象
		$.extend(icthis, options);
		if(!options.input)
			return null;
		options.input = $(options.input);
		//创建下拉层
		icthis.box = $("<div>");
		icthis.box.css({
			position: "absolute",
			zIndex: "1000"
		});
		//添加内容
		if(options.content)
			icthis.setContent(options.content);
		//把层设置为隐藏
		icthis.hide();
		icthis.box.addClass("input-contents");
		//调整坐标的函数
		this.adjustPos = function (){
			var position = options.input.position();
			icthis.box.css({
				left: position.left + "px",
				top: position.top + options.input.outerHeight() + 2 + "px",
				height: options.height ? options.height + "px" : "192px",
				width: options.input.innerWidth() + "px"
			});
		};
		//内部方法
		this.funs = {
				autoHide: function () {
					icthis.hide();
				},
				stopPropagation: function (event) {
					event.stopPropagation();
				}
		};
		//设置自动隐藏
		this.autoHide(true);
		//给input添加事件
		options.input.bind("click", function () {
			icthis.show();
			return false;
		});
		if(options.readonly == true)
			options.input.attr("readonly", true);
		//整个内容加入body
		$(document.body).append(icthis.box);
	};
	
	/**
	 * 销毁
	 * @return
	 */
	InputContents.prototype.destroy = function () {
		this.box.remove();
	};
	
	/**
	 * 显示
	 * @return
	 */
	InputContents.prototype.show = function () {
		this.adjustPos();
		this.box.show();
	};
	
	/**
	 * 隐藏
	 * @return
	 */
	InputContents.prototype.hide = function () {
		this.box.hide();
	};
	
	/**
	 * 取得主容器
	 * @return
	 */
	InputContents.prototype.getBox = function () {
		return this.box;
	};
	
	/**
	 * 设置内容
	 * @param content
	 * @return
	 */
	InputContents.prototype.setContent = function (content) {
		this.box.empty().append(content);
	};
	
	/**
	 * 设置是否自动隐藏
	 * @param autoHide
	 * @return
	 */
	InputContents.prototype.autoHide = function (autoHide) {
		var icthis = this;
		if(autoHide) {
			$(document.documentElement).bind("click", icthis.funs.autoHide);
			icthis.box.bind("click", icthis.funs.stopPropagation);
		} else {
			$(document.documentElement).unbind("click", icthis.funs.autoHide);
			icthis.box.unbind("click", icthis.funs.stopPropagation);
		}
	};
	
	window.InputContents = InputContents;
})();
/**
 * 联系薄2.0
 */
;(function () {
	/**
	 * 
	 */
	var SERIAL_NUMBER = 0;
	function AccountBook(options)
	{
		//这个联系人本唯一的标识,为支持同一页面存在多个联系人选择器。
		this.uniqueNumber= ++ SERIAL_NUMBER;
		var abthis = this;
		$.extend(abthis, options);
		if(!abthis.input)
			return null;
		//转换为jQuery对象
		abthis.input = $(abthis.input);
		//input的name
		abthis.paramName = abthis.input.attr("name");
		abthis.input.removeAttr("name");
		abthis.inputContents = new InputContents({
			input: $(abthis.input),
			readonly: true
		});
		abthis.form = abthis.form ? $(abthis.form) : abthis.input.parents("form:first");
		//给选择模式一个默认值
		abthis.mode = abthis.mode ? abthis.mode : "single";
		//需要添加的事件列表
		var events = {};
		events.dblclick = events.checkChanged = function () {
			var selectedList = abthis.getSelectedList();
			//清除数据
			if(abthis.mode != "single")
			{
				abthis.form.find(".selected-" + abthis.uniqueNumber).remove();
				abthis.input.val("");
			}
			var selectedNum = selectedList.length;
			//把选择的数据添加进form
			//单选和多选得到的数据，均为数组对待，所以循环中要做模式判断。
			for(var i=0;i<selectedNum;++i)
			{
				var $selected = $(selectedList[i]);
				if($selected.parents("span:first").siblings(".jqtree-has-children").size() <= 0)
				{
					if(abthis.mode == "single")
					{
						abthis.form.find(".selected-" + abthis.uniqueNumber).remove();
						abthis.input.val("");
					}
					abthis.form.append("<input class='selected-" + abthis.uniqueNumber + "' type='hidden' name='" + abthis.paramName + "' value='" + $selected.attr("id") + "'/>");
					abthis.input.val(abthis.input.val() + $selected.html() + ";");
				}
				else
				{
					if(abthis.mode == "single")
						return false;
				}
			}
			//隐藏,只有单选模式、双击后才会自动隐藏.
			if(abthis.mode == "single")
				abthis.inputContents.hide();
		};
		//联系人树
		abthis.tree = new jQuery.Tree({
			selectMode: abthis.mode,
			source: abthis.data,
			pretreatment: abthis.pretreatment,
			parent: abthis.inputContents.getBox(),
			nodeEvents: events
		});
		//展开树
		abthis.tree.openAll();
		//调整样式
		abthis.inputContents.getBox().find("ul.jq-tree")
		.css({
			margin: "4px",
			"-moz-user-select": "none",	//禁止选择@moz
			"-webkit-user-select": "none"	//禁止选择@webkit
		})
		.bind("selectstart", function () {	//禁止选择@ie
			return false;
		});
		//默认选择
		if(abthis.defaultValue)
			this.select(abthis.defaultValue);
	};
	
	/**
	 * 取得选择的数据列表
	 * @return
	 */
	AccountBook.prototype.getSelectedList = function () {
		if(this.tree.selectMode == "single")
			return [this.tree.getSelectedContent()];
		else
		{
			return this.tree.getSelectedContentList();
		}
	};
	
	AccountBook.prototype.select = function (datas) {
		var abthis = this;
		var allNodeContent =  this.inputContents.getBox().find("span.jqtree-node-content");
		allNodeContent.each(function () {
			var $this = $(this);
			if($.inArray(parseInt($this.attr("id")), datas) != -1)
			{
				if(abthis.mode == "checkbox")
				{
					$this.siblings("input.jqtree-ms-box").trigger("click");
					$this.siblings("input.jqtree-ms-box").siblings("span.jqtree-node-content").trigger("checkChanged");
				}
				else
				{
					$this.trigger("mousedown");
					$this.trigger("dblclick");
				}
			}
		});
	};
	
	window.AccountBook = AccountBook;
})();